package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.SkuInfo;
import com.changgou.search.dao.ESManagerMapper;
import com.changgou.search.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ESManagerServiceImpl implements ESManagerService {

    //先注入ES模板
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    //需要导入feign
    @Autowired
    private SkuFeign skuFeign;

    //导入自定义的接口
    @Autowired
    private ESManagerMapper esManagerMapper;

    //创建索引库,而且还是必须要第一步操作的
    @Override
    public void createMappingAndIndex() {
        //创建索引
        elasticsearchTemplate.createIndex(SkuInfo.class);
        //创建映射
        elasticsearchTemplate.putMapping(SkuInfo.class);
    }

    @Override
    public void importAll() {
        //查询sku集合
        //括号里面的是填入当时在feign写入判断的all
        List<Sku> skuList = skuFeign.findSkuListBySpuId("all");
        if (skuList == null || skuList.size() <= 0){
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库!");
        }

        //查到数据
        //将集合转为josn
        String jsonSkuList = JSON.toJSONString(skuList);
        //将json转换为skuinfo
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);

        //遍历以后将规格信息转换为map
        for (SkuInfo skuInfo : skuInfoList) {
            Map map = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(map);
        }

        //导入索引库
        esManagerMapper.saveAll(skuInfoList);

    }

    @Override
    public void importDataBySpuId(String spuId) {
//查询sku集合
        //括号里面的是填入当时在feign写入判断的all
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (skuList == null || skuList.size() <= 0){
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库!");
        }

        //查到数据
        //将集合转为josn
        String jsonSkuList = JSON.toJSONString(skuList);
        //将json转换为skuinfo
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);

        //遍历以后将规格信息转换为map
        for (SkuInfo skuInfo : skuInfoList) {
            Map map = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(map);
        }

        //导入索引库
        esManagerMapper.saveAll(skuInfoList);
    }

    @Override
    public void delDataBySpuId(String spuId) {
        //获取skufeign里面的list数据
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        //判断当前商品是否存在
        if (skuList == null || skuList.size() <= 0){
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库!");
        }
        //存在遍历liat集合,获取spuid
        for (Sku sku : skuList) {
            //由于这个ID是long类型的,所以需要转换类型
            esManagerMapper.deleteById(Long.parseLong(sku.getId()));
        }

    }
}
